package day19.pack8;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Station {
    private int count = 100;
    private Lock lock = new ReentrantLock(true);

    public void sell() {
        boolean res = false;
        try {
//            lock.lock();  傻等，拿不到锁就一直等
            res = lock.tryLock(1, TimeUnit.MILLISECONDS);
            if(res) {
                if (count > 0) {
                    System.out.println("销售座位" + count-- + ",该票出售给了：" + Thread.currentThread().getName());
                } else {
                    System.out.println("抱歉，剩余票数为：" + count + ",当前线程为：" + Thread.currentThread().getName());
                }
            } else {
                System.out.println(Thread.currentThread().getName() + "没拿到.moyu...");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(res) lock.unlock();
        }
    }
}
